﻿@using Microsoft.AspNetCore.Components.Authorization
@inject IStringLocalizer<BlockUsers> Localizer
<div style="height: 21px;">
    <Block Users="@Users">
        <div>@((MarkupString)Localizer["UsersDiv1", UserName].Value)</div>
    </Block>
</div>
<Button Icon="@GetUser()" Text="@GetUserText()" OnClick="ToggleAuthor" class="mt-3" />

@code {
    [Inject]
    [NotNull]
    private AuthenticationStateProvider? AuthenticationStateProvider { get; set; }

    private bool IsAuth { get; set; }

    private IEnumerable<string> Users { get; } = new string[] { "BootstrapBlazor" };

    [NotNull]
    private string? UserName { get; set; }
    private string GetUser() => IsAuth ? "fa-solid fa-user-secret" : "fa-solid fa-user";
    private string GetUserText() => IsAuth ? Localizer["Logout"] : Localizer["Login"];
    private async Task ToggleAuthor()
    {
        if (AuthenticationStateProvider is MockAuthenticationStateProvider mock)
        {
            if (!IsAuth)
            {
                mock.Login();
                var state = await mock.GetAuthenticationStateAsync();
                UserName = state.User.Identity?.Name;
                IsAuth = state.User.Identity?.IsAuthenticated ?? false;
            }
            else
            {
                mock.Logout();
                IsAuth = false;
                UserName = "";
            }
        }
    }
}
